#!/usr/bin/perl

use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use lib qw( /var/pwneth/htdocs/rpg/lib );
use RedMoon::Account;
use RedMoon::Utils;
use RedMoon::Core;
use RedMoon::Main;
use RedMoon::Dungeon;

my $q = CGI->new;

my $pwn = RedMoon::Account->new;
my $game = RedMoon::Core->new;
my $main = RedMoon::Main->new;
my $dng = RedMoon::Dungeon->new;
my $utils = RedMoon::Utils->new;

my $acct_id = $q->param('id');
my $func = $q->param('f');
my $mth = $q->param('m')||'Main';
my $action = $q->param('a');
my $session;

($acct_id,$session) = $pwn->isLoggedIn($acct_id);

		unless($acct_id && $session)
		{
			print $q->header;
			print "<script type='text/javascript'>
					<!--
					parent.location.reload();
					//-->
					</script>";
		}

			print $q->header();
my($html,$char);

for my$f ($func)
{
	if($f =~ /mod/i)
	{

	$html = $main->LoadModule($mth,$action);

	}
	elsif($f =~ /dungeon/i)
	{

	$html = $dng->LoadDungeon($mth,$action);

	}

	($char) = $game->char_menu($acct_id);
	$html =~ s/\$ACCT_ID\$/$acct_id/g;
	$html =~ s/\$CHAR\$/$char/g;
}

if($action =~ /inv/i)
{
	my%inv = $game->getCharacterInv_Disp($acct_id);

		foreach my $k (keys %inv)
		{
			next if($k eq 'bags');
			$inv{$k} .= "</td><td><sub><a href='' onclick=\"runTask('unequip','$acct_id','$k');loadModule('Main', '$acct_id', 'Inv');\">remove</a></sub>"
			if($inv{$k});
			$inv{$k} .= "</td></tr>";
		}

	$html =~ s/\$PRI\$/$inv{phand}/g;
	$html =~ s/\$OFF\$/$inv{ohand}/g;
	$html =~ s/\$HEAD\$/$inv{head}/g;
	$html =~ s/\$NECK\$/$inv{neck}/g;
	$html =~ s/\$BODY\$/$inv{body}/g;
	$html =~ s/\$BACK\$/$inv{back}/g;
	$html =~ s/\$ARMS\$/$inv{arms}/g;
	$html =~ s/\$HANDS\$/$inv{hands}/g;
	$html =~ s/\$ACC1\$/$inv{acc1}/g;
	$html =~ s/\$ACC2\$/$inv{acc2}/g;

	my $bags;
	my $n;

	my@bags = $inv{bags};

	foreach my $b (@bags)
	{
		$n++;
		next if($b eq '');
		$bags .= "<tr><td>$n</td><td>$b</td><td><sub><a href='' onclick=\"runTask('equip','$acct_id','$b');\">equip</a></sub></td></tr>"; # INSERT EQUIP
	}

	$html =~ s/\$INV\$/$bags/g;

}
#else
#{

#}

	print "$html";
